#include "PatternFileManager.h"
#include <fstream>

PatternFileManager::PatternFileManager()
: _filename(""), _totalposition(0), _size(0)
{}

PatternFileManager::~PatternFileManager()
{}

bool					PatternFileManager::IsFileExist() const
{
	std::ifstream		is;

	is.open(_filename.c_str());
	if (!is.is_open()) 
	{
		std::cerr << "PatternFileManager : " << this->_filename << " : No such file or directory" << std::endl;
		return (false);
	}
	is.close();
	return (true);
}

const int &				PatternFileManager::GetTotalPosition() const
{
	return (this->_totalposition);
}

const int &				PatternFileManager::GetSize() const
{
	return (this->_size);
}

void					PatternFileManager::GetContent(std::list<std::string> & content)
{
	if (!IsFileExist()) return;
	std::ifstream		is;
	char				ctr[20];

	is.open(this->_filename.c_str());
	while(is.good())
	{
		is.getline(ctr, 20);
		std::string	str = ctr;
		content.push_back((std::string)str);
	}
	is.close();
	if (!content.empty())
	{
		this->SetTotalPosition(content);
		this->SetSize(content);
	}
}

void					PatternFileManager::SetTotalPosition(const std::list<std::string> & content)
{
	std::list<std::string>::const_iterator	itb = content.begin(), ite = content.end();
	int				res = 0;
	
	for(; itb != ite; ++itb)
	{
		if ((*itb).length() == 0) ++res;
	}
	this->_totalposition = res + 1;
}

void					PatternFileManager::SetSize(const std::list<std::string> & content)
{
	std::string str = content.front();
	this->_size = str.length();
}

void					PatternFileManager::LoadFile(const std::string & filename, std::list<std::string> & content)
{
	this->_filename = filename;
	this->GetContent(content);
}